const axios = require('../https.js');
const axiosData = require('../api/axios.js');
const logger = require('../logger');

module.exports = {
    //首页
    index: (req,res,next) => {
        axios.all([axiosData.userInfo(req.session.token),axiosData.accountDetail(req.session.token)])
            .then(axios.spread(function(inforData,accountData){
            const result1 = inforData.data;
            const result2 = accountData.data;
            if(result1.success && result2.success){
                let renderData = {
                    title: '账户管理',
                    menuActive:[0,0],
                    name: result1.data.financier.legalName,
                    type: result1.data.financier.type,
                    accountMoney: result2.data.financierAccount,
                    totalPage: result2.data.count
                };
                res.render('site/index',renderData)
            }else{
                if(!result1.success){
                    logger.error('borrow/financier/info 接口出错：'+result1.cause);
                }
                if(!result2.success){
                    logger.error('borrow/financier/account/info 接口出错：'+result2.cause);
                }
                if(result1.code == '1001' || result1.success == '1004' || result2.code == '1001' || result2.code == '1004'){
                    req.session.destroy();
                    res.redirect('/login');
                }else{
                    res.render('404',{
                        layout: false
                    })
                }
            }
        })).catch(function(error){
            if(error.response){
                logger.info('页面渲染报错：/borrow/financier/info接口或/borrow/financier/account/info， 存在请求，但是服务器返回一个状态吗，在2xx之外');
            }else{
                logger.error('页面渲染报错：'+error + ' from 首页接口：/borrow/financier/info 或 /borrow/financier/account/info');
            }
            res.render('404',{
                layout: false
            })
        });
    },
    flowing: (req,res,next) => {
        axios({
            url: '/borrow/financier/info',
            method: 'get',
            params: {
                token: req.session.token
            }
        })
            .then(function(response){
                var result = response.data;
                if(result.success){
                    let renderData = {
                        title: '账户流水',
                        menuActive:[0,1],
                        name: result.data.financier.legalName,
                        type: result.data.financier.type
                    };
                    res.render('site/flowing',renderData)
                }else{
                    logger.error('borrow/financier/info 接口出错！'+ response.data.code);
                    if(result.code == '1001' || result.code == '1004'){
                        req.session.destroy();
                        res.redirect('/login');
                    }else{
                        res.render('404',{
                            layout: false
                        })
                    }
                }
            })
            .catch(function(error){
                if(error.response){
                    logger.info('页面渲染报错：/borrow/financier/info接口， 存在请求，但是服务器返回一个状态吗，在2xx之外');
                }else{
                    logger.error('页面渲染报错：'+error + ' from 账户流水页接口：/borrow/financier/info');
                }
                res.render('404',{
                    layout: false
                })
            });
    },
    information: (req,res,next) => {
        axios({
            url: '/borrow/financier/info',
            method: 'get',
            params: {
                token: req.session.token
            }
        })
            .then(function(response){
                var result = response.data;
                if(result.success){
                    let renderData = {
                        title: '个人信息',
                        menuActive:[1,0],
                        name: result.data.financier.legalName,
                        type: result.data.financier.type,
                        informationDetail: result.data.financier,
                        file: result.data.file,
                        userTelPhone: req.session.userPhone
                    };
                    res.render('site/information',renderData)
                }else{
                    logger.error('borrow/financier/info 接口出错！'+ response.data.code);
                    if(result.code == '1001' || result.code == '1004'){
                        req.session.destroy();
                        res.redirect('/login');
                    }else{
                        res.render('404',{
                            layout: false
                        })
                    }
                }
            })
            .catch(function(error){
                if(error.response){
                    logger.info('页面渲染报错：/borrow/financier/info接口， 存在请求，但是服务器返回一个状态吗，在2xx之外');
                }else{
                    logger.error('页面渲染报错：'+error + ' from 个人信息页接口：/borrow/financier/info');
                }
                res.render('404',{
                    layout: false
                })
            });
    },
    change: (req,res,next) => {
        axios({
            url: '/borrow/financier/info',
            method: 'get',
            params: {
                token: req.session.token
            }
        })
            .then(function(response){
                let result = response.data;
                if(result.code=='200'){
                    let renderData = {
                        title: '修改登录密码',
                        menuActive:[1,1],
                        name: result.data.financier.legalName,
                        type: result.data.financier.type
                    };
                    res.render('site/change',renderData)
                }else{
                    logger.error('borrow/financier/info 接口出错！'+ response.data.code);
                    if(result.code == '1001' || result.code == '1004'){
                        req.session.destroy();
                        res.redirect('/login');
                    }else{
                        res.render('404',{
                            layout: false
                        })
                    }
                }
            })
            .catch(function(error){
                if(error.response){
                    logger.info('页面渲染报错：/borrow/financier/info接口， 存在请求，但是服务器返回一个状态吗，在2xx之外');
                }else{
                    logger.error('页面渲染报错：'+error + ' from 修改密码接口：/borrow/financier/info');
                }
                res.render('404',{
                    layout: false
                })
            });
    },
    find: (req,res,next) => {
        let renderData = {
            title: '找回登录密码'
        };
        res.render('site/find',renderData)
    },
    detail: (req,res,next) => {
        axios.all([axiosData.userInfo(req.session.token),axiosData.detailList(req.session.token,req.params.id)])
            .then(axios.spread(function(infoData,detailData){
                let result1 = infoData.data;
                let result2 = detailData.data;
                if(result1.success && result2.success){
                    let renderData = {
                        title: '详情',
                        menuActive:[0,0],
                        name: result1.data.financier.name,
                        type: result1.data.financier.type,
                        detail: result2.data.borrow,
                        borrowerInfo: result2.data.file
                    };
                    res.render('site/detail',renderData);
                }else{
                    if(!result1.success){
                        logger.error('页面渲染报错：borrow/financier/info 接口出错！'+ result1.code);
                    }
                    if(!result2.success){
                        logger.error('页面渲染报错：/borrow/borrow/info 接口出错！'+ result2.code);
                    }
                    if(result1.code == '1001' || result1.code == '1004' || result2.code == '1001' || result2.code == '1004'){
                        req.session.destroy();
                        res.redirect('/login');
                    }else{
                        res.render('404',{
                            layout: false
                        })
                    }
                }
            }))
            .catch(function(error){
                if(error.response){
                    logger.info('页面渲染报错：/borrow/financier/info接口或/borrow/borrow/info接口， 存在请求，但是服务器返回一个状态吗，在2xx之外');
                }else{
                    logger.error('页面渲染报错：'+error + ' from 详情页接口：/borrow/financier/info或/borrow/borrow/info');
                }
                res.render('404',{
                    layout: false
                })
            });
    },
    login: (req,res,next) => {
        let renderData = {
            title: '登陆'
        };
        res.render('site/login',renderData)
    }
};